<?php
defined('BASEPATH') OR exit('No direct script access allowed');
/* 
	报名记录
*/
class Signup_record extends Console_Controller{
	
	private $school_id; 
	
	public function __construct(){
		parent::__construct();
		$this->load->model('signup_record_model');
		$this->school_id = $this->user['school_id'];
	}
	
	public function index(){
		$gets = $this->input->get();
		$assign = [
			'gets' => $gets,
			'options' => $this->signup_record_model->dict_options()
		];
		$keyword = $gets['keyword'];
		$where = [];
		// 学校ID
		if($this->school_id){
			$where['school_id'] = $this->school_id;
		}
		if(isset($gets['status']) && $gets['status'] !== ''){
			$where['status'] = $gets['status'];
		}
		if($keyword){
			if(is_numeric($keyword)){
				$where['parent_phone like'] = "%$keyword%";
			}else{
				$where['student_name like'] = "%$keyword%";
			}
		}
		$total = $this->signup_record_model->get_total($where);
		if($total){
			$this->load->library('pagination', [
				'total_num' => $total
			]);
			$assign['show_page'] = $this->pagination->create();
			$assign['list'] = $this->signup_record_model->get_result([
				'where' => $where,
				'start' => $this->pagination->limit_start,
				'limit' => $this->pagination->page_size
			]);
		}
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('signup_record.htm');
	}
	
	public function detail(){
		$this->render('detail');
	}
	
	private function render($action){
		$assign = array(
			'action' => $action,
			'gets' => $this->input->get(),
			'options' => $this->signup_record_model->dict_options()
		);
		switch($action){
			case 'detail':
				$id = $this->input->get('id');
				$detail = $this->signup_record_model->get_row($id);
				if(!$detail){
					$this->show_message();
				}
				$assign['detail'] = $detail;
				$distpl = 'signup_record_detail.htm';
				break;
			default:
				$this->show_message();
		}
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display($distpl);
	}
	
	// 审核通过
	public function passed(){
		$this->action('passed');
	}
	
	private function action($method){
		$actids = $this->input->post('actids');
		if(! $actids){
			$this->output_json();
		}
		switch($method){
			case 'passed':
				$sendsms = $this->sendsms_passed();
				if(!$sendsms){
					$this->output_json('短信发送失败');
				}
				$query = $this->signup_record_model->set([
					'status' => 1
				], [
					'where_in' => [
						'id' => $actids
					]
				]);
				break;
			default:
				$this->output_json();
		}
		if($query){
			$this->output_json('操作成功', 0);
		}
		$this->output_json('操作失败');
	}
	
	// 发送审核通过短信
	private function sendsms_passed(){
		$res = $this->signup_record_model->get_result([
			'select' => ['student_name', 'parent_name', 'parent_phone'],
			'where' => [
				'status' => 0
			],
			'where_in' => [
				'id' => $this->input->post('actids')
			]
		]);
		if(!$res){
			return false;
		}
		foreach($res as $row){
			$phones[] = $row['parent_phone'];
			$data[] = [
				'parent' => $row['parent_name'],
				'student' => $row['student_name']
			];
		}
		$this->load->library('Aliyun/smslib');
		return $this->smslib->batch_send_notice($phones, $data, 'signup_passed');
	}
	
	// 全部导出
	public function export(){
		$this->load->library('myexcel');
		$title = '报名表';
		if($this->school_id){
			$where['school_id'] = $this->school_id;
		}
		$data = $this->signup_record_model->get_result($where);
		$options = $this->signup_record_model->dict_options();
		foreach($data as &$row){
			$row['student_sex'] = $options['sex'][$row['student_sex']];
			$row['inputtime'] = date('Y-m-d', $row['inputtime']);
			$row['campus_adjustment'] = $options['bool'][$row['campus_adjustment']];
			$row['study_way'] = $options['study_way'][$row['study_way']];
			$row['status'] = $options['status'][$row['status']];
		}
		if($this->school_id){
			$title = $data[0]['school'];
		}
		$this->myexcel->write([
			'title' => $title,
			'fields' => [
				'inputtime' => '报名时间',
				'school' => '报名学校',
				'grade' => '报名年级',
				'campus' => '报名学区',
				'campus_adjustment' => '是否服从校区调剂',
				'study_way' => '就读方式',
				'student_name' => '学生姓名',
				'student_idno' => '身份证号',
				'student_sex' => '性别',
				'student_birthday' => '出生日期',
				'nation' => '民族',
				'height' => '身高',
				'weight' => '体重',
				'live_address' => '现居住地址',
				'family_address' => '家庭地址',
				'now_school' => '现就读学校',
				'now_grade' => '现就读年级',
				'self_appraise' => '自我评价',
				'honors' => '获得荣誉',
				'father_name' => '父亲姓名',
				'father_phone' => '父亲电话',
				'father_company' => '父亲工作单位',
				'mother_name' => '母亲姓名',
				'mother_phone' => '母亲电话',
				'mother_company' => '母亲工作单位',
				'parent_name' => '联系人',
				'parent_phone' => '联系电话',
				'parent_clan' => '与学生关系',
				'parent_email' => '联系邮箱',
				'follow_way' => '获知学校途径',
				'remark' => '备注留言',
				'status' => '审核状态'
			]
		], $data);
	}
	
}